package top.hesisi.springboot3.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.hesisi.springboot3.base.PageBean;
import top.hesisi.springboot3.base.R;
import top.hesisi.springboot3.dao.TbSoftwareTypeMapper;
import top.hesisi.springboot3.entity.TbSoftwareType;
import top.hesisi.springboot3.service.TbSoftwareTypeService;
import top.hesisi.springboot3.vo.PageVO;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class TbSoftwareTypeServiceImpl implements TbSoftwareTypeService {
    private final TbSoftwareTypeMapper tbSoftwareTypeMapper;
    private TbSoftwareType queryByTname(String tname) {
        QueryWrapper<TbSoftwareType> wrapper = new QueryWrapper<>();
        wrapper.eq("tname", tname);
        //查询单一记录的方法
        TbSoftwareType tbSoftwareType = tbSoftwareTypeMapper.selectOne(wrapper);
        return tbSoftwareType;
    }


    @Override
    public R<String> insert(TbSoftwareType tbSoftwareType) {
        TbSoftwareType check = queryByTname(tbSoftwareType.getTname());
        if (check != null) {
            return R.fail("软件类型已存在", null);
        }
        int i = tbSoftwareTypeMapper.insert(tbSoftwareType);
        return i==1 ? R.ok("添加成功", null) : R.fail("添加失败", null);
    }


    @Override
    public R<String> update(TbSoftwareType tbSoftwareType) {
        TbSoftwareType check = queryByTname(tbSoftwareType.getTname());
        if (check != null && !check.getId().equals(tbSoftwareType.getId())) {
            return R.fail("软件类型已存在", null);
        }
        int i = tbSoftwareTypeMapper.updateById(tbSoftwareType);
        return i==1 ? R.ok("修改成功", null) : R.fail("修改失败", null);
    }


    @Override
    public R<String> delete(Integer id) {
        int i = tbSoftwareTypeMapper.deleteById(id);
        return i==1 ? R.ok("删除成功", null) : R.fail("删除失败", null);
    }


    @Override
    public R<TbSoftwareType> queryById(Integer id) {
        TbSoftwareType tbSoftwareType = tbSoftwareTypeMapper.selectById(id);
        return tbSoftwareType != null ? R.ok("查询成功", tbSoftwareType) : R.fail("查询失败", null);
    }


    @Override
    public R<List<TbSoftwareType>> query(TbSoftwareType tbSoftwareType) {
        QueryWrapper<TbSoftwareType> wrapper = new QueryWrapper<>();
        wrapper.like(
                StringUtils.hasText(tbSoftwareType.getTname()),
                "tname",
                tbSoftwareType.getTname()
        );
        wrapper.like(
                StringUtils.hasText(tbSoftwareType.getIntro()),
                "intro",
                tbSoftwareType.getIntro()
        );
        List<TbSoftwareType> list = tbSoftwareTypeMapper.selectList(wrapper);
        return R.ok(list);
    }


    @Override
    public R<PageVO<TbSoftwareType>> queryPage(PageBean pageBean, TbSoftwareType tbSoftwareType) {
        IPage<TbSoftwareType> page = new Page<>(pageBean.getPageNumber(), pageBean.getPageSize());
        QueryWrapper<TbSoftwareType> wrapper = new QueryWrapper<>();
        wrapper.like(
                StringUtils.hasText(tbSoftwareType.getTname()),
                "tname",
                tbSoftwareType.getTname()
        );
        wrapper.like(
                StringUtils.hasText(tbSoftwareType.getIntro()),
                "intro",
                tbSoftwareType.getIntro()
        );
        page = tbSoftwareTypeMapper.selectPage(page,wrapper);
        return R.ok(page);
    }
}
